import { type RouteObject } from "react-router-dom"
import Home from "@/pages"
import { redirect } from "react-router-dom"
import Documents from "@/pages/documents"
import Memories from "@/pages/memories"
import Visitors from "@/pages/visitors"
import Layout from "@/layout"
import Log from "@/pages/settings/log"
import Login from "@/pages/login"
import { authAlign } from "@/services"
import { message } from "antd"
import {
  ContainerOutlined,
  DashboardOutlined,
  FileTextOutlined,
  HeartOutlined,
  SettingOutlined,
  TeamOutlined,
} from "@ant-design/icons"
import React from "react"
import Tracks from "@/pages/visitors/tracks"

export default [
  {
    path: "/",
    Component: Layout,
    name: "",
    loader: async () => {
      try {
        const res: any = await authAlign()
        if (res?.status === 200) {
          return res.data
        }
      } catch (e) {
        throw redirect("/login")
      }
      return null
    },
    children: [
      {
        path: "/",
        name: "首页",
        Component: Home,
        icon: React.createElement(DashboardOutlined),
      },
      {
        path: "/documents",
        Component: Documents,
        name: "文档",
        icon: React.createElement(ContainerOutlined),
      },
      {
        path: "/memories",
        Component: Memories,
        name: "记忆",
        icon: React.createElement(HeartOutlined),
      },
      {
        path: "/visitors",
        name: "访客",
        icon: React.createElement(TeamOutlined),
        hideChildrenInMenu: true,
        children: [
          {
            index: true,
            Component: Visitors,
          },
          {
            path: "tracks",
            Component: Tracks,
          },
        ],
      },
      {
        path: "/logs",
        name: "日志",
        icon: React.createElement(FileTextOutlined),
        Component: Log,
      },
    ],
  },
  {
    path: "/login",
    Component: Login,
    hideInMenu: true,
    loader: async () => {
      try {
        const res: any = await authAlign()
        if (res?.status === 200) {
          throw new Error("1")
        }
      } catch (e: any) {
        if (e?.message === "1") {
          message.info("已登录")
          throw redirect("/")
        }
      }
      return null
    },
    name: "登录",
  },
] as Array<RouteObject & { [key: string]: any }>
